package com.sunnada.edu.system.org.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sunnada.edu.system.org.bean.Org;
import com.sunnada.edu.system.org.service.OrgService;
import com.sunnada.edu.system.pub.common.BasicController;
import com.sunnada.edu.system.pub.common.Tree;
import com.sunnada.edu.system.pub.common.TreeMethods;
import com.sunnada.edu.system.pub.dao.Page;
import com.sunnada.edu.system.pub.service.PubMethods;
import com.sunnada.edu.system.pub.util.Utils;


/**
 * explain 控制类
 * 
 * @author chs 2015-5-12 version 1.0
 */
@Controller
@RequestMapping("/orgController")
public class OrgController extends BasicController {
	@Resource
	OrgService orgService;

	/** 区域树 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/orgTree")
	public void orgTree(HttpServletResponse response, HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Org.class, params);
		List list = orgService.orgTree(params);
		List treeList = new ArrayList<Tree>();
		treeList = TreeMethods.setOrgTreeProperty(list, true);
		printInfo(response, JSONArray.toJSONString(treeList).toString());
	}

	/** 区域管理首页 zjc 2016-08-11 */
	@RequestMapping("/org")
	public String org(HttpServletRequest request) {
		// 当前路径
		String moduleId = request.getParameter("moduleId");
		request.setAttribute("pathValue", PubMethods.getPathValue(moduleId));
		return "system/org/org";
	}

	/** 区域增加 zjc 2016-08-10 */
	@RequestMapping("/orgAddshow")
	public String orgAddshow(HttpServletRequest request) {
		return "system/org/org_add";
	}

	/** 区域保存功能 zjc 2016-08-10 */
	@RequestMapping("/orgSave")
	public void orgSave(HttpServletResponse response, HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Org.class, params);
		String recId = params.get("recId");
		if (Utils.isNull(recId)) {
			msg = orgService.orgAdd(params);
		} else {
			msg = orgService.orgUpdate(params);
		}
		printInfo(response, msg);
	}

	/** 区域修改界面 zjc 2016-08-10 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("/orgUpdateShow")
	public String orgUpdateShow(HttpServletRequest request, HttpServletResponse repoResponse) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Org.class, params);
		Map org = orgService.getOrgByRecId(params);
		request.setAttribute("org", org);
		return "system/org/org_update";
	}

	/** 区域修改功能 zjc 2016-08-10 */
	@RequestMapping("/orgDelete")
	public void orgDelete(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds", Utils.getSqlSplitStr(recIds));
		msg = orgService.orgDel(params);
		printInfo(response, msg);
	}

	@RequestMapping("/orgTreebig")
	public void orgTreebig(HttpServletResponse response, HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Org.class, params);
		String chrild = orgService.orgTreebig(params);
		printInfo(response, chrild);
	}

	/** 区域查询 zjc 2015-05-13 */
	@RequestMapping("/orgSearch")
	public void orgSearch(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Org.class, params);
		Page page = new Page();
		page.setRowCount(orgService.getOrgCots(params));
		page.setPageMap(request, page, params);
		List<?> orgList = orgService.findOrgs(params);
		JSONObject json = new JSONObject();
		json.put("list", orgList);
		json.put("params", params);
		printInfo(response, json);
	}

	/** 根据区域orgCode查询区域信息 zjc 2015-05-14 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("/orgByOrgcode")
	public void orgByOrgcode(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		String orgCode = request.getParameter("orgCode");
		params.put("orgCode", orgCode);
		Map org = orgService.orgByOrgcode(params);
		JSONObject json = new JSONObject();
		json.put("org", org);
		printInfo(response, json);
	}

	/** 区域启用功能 zjc 2015-5-26 */
	@RequestMapping("/orgEnable")
	public void orgEnable(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds", Utils.getSqlSplitStr(recIds));
		msg = orgService.orgEnable(params);
		printInfo(response, msg);
	}

	@RequestMapping("/orgDisable")
	public void orgDisable(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds", recIds);
		msg = orgService.orgDisable(params);
		printInfo(response, msg);
	}

}
